import { defineStore } from "pinia";

export interface IAppGlobalState {
  /**
   * 用户是否登录
   */
  isLogin: boolean;
  /**
   * 安全区域 - 在竖屏正方向下的安全区域
   */
  safeArea: {
    bottom: number;
    height: number;
    left: number;
    right: number;
    top: number;
    width: number;
  };
  /**
   * 安全区域 - 在竖屏正方向下的安全区域插入位置
   */
  safeAreaInsets: {
    bottom: number;
    left: number;
    right: number;
    top: number;
  };
}

export const appGlobal = defineStore({
  id: "appGlobal",
  // 转换为函数
  state: () => ({
    userInfo: {} as any,
    isLogin: false as boolean,
    safeArea: {
      bottom: 0,
      height: 0,
      left: 0,
      right: 0,
      top: 0,
      width: 0,
    } as IAppGlobalState["safeArea"],
    safeAreaInsets: {
      bottom: 0,
      height: 0,
      left: 0,
      right: 0,
      top: 0,
      width: 0,
    } as IAppGlobalState["safeAreaInsets"],
  }),
  actions: {
    save(payload: any) {
      this.safeArea = payload.safeArea;
      this.safeAreaInsets = payload.safeAreaInsets;
      this.isLogin = payload.isLogin;
    },
    // 清除用户信息
    clear(payload: any) {
      this.userInfo = payload as any;
    },
  },
});
